package com.gzhryc.shared_device.cdcz.code.services;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.cdcz.code.dao.CDCZSlotInstantLogDao;
import com.gzhryc.shared_device.cdcz.code.dao.db.CDCZSlotInstantLog;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class CDCZSlotInstantLogService {

    static Logger log = Logger.getLogger(CDCZSlotInstantLogService.class);

    public static CDCZSlotInstantLogService self() {
        return MultiDBTools.getService(CDCZSlotInstantLogService.class, true);
    }

    public static CDCZSlotInstantLogService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, CDCZSlotInstantLogService.class, true);
    }

    CDCZSlotInstantLogDao dao;

    public CDCZSlotInstantLogService(String jdbcKey) {
        dao = new CDCZSlotInstantLogDao(jdbcKey);
    }

    public List<CDCZSlotInstantLog> findByKeysAndDate(String deviceSn, Integer slotNum, Date startDate, Date endDate){
        if(StringTools.isNotBlank(deviceSn) && slotNum != null && startDate != null && endDate != null){
            Conditions conditions = new Conditions(CDCZSlotInstantLog.class);
            conditions.and().is("deviceSn",deviceSn);
            conditions.and().is("slotNum",slotNum);
            conditions.and().ge("createDate",startDate);
            conditions.and().le("createDate",endDate);

            try {
                return dao.findByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public boolean add(CDCZSlotInstantLog instantLog){
        if(instantLog.getCreateDate() != null){
            try {
                return dao.insert(instantLog) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return false;
    }
}
